package com.mymaxis.ebppx;

import java.io.*;
import java.sql.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.checkfree.isolutions.persist.*;
import com.checkfree.isolutions.utility.CFiError;
import com.checkfree.isolutions.property.Properties;
import com.checkfree.isolutions.property.PropertiesConfig;

/**

* The EBPS Customer Application Servlet.

*/

public class CusConnectionPoolServlet extends HttpServlet {

    public static ConnectionPool connectionPool;
    public static boolean connectionPoolStarted = false;

	public void init() {

		connectionPool = null;

		try {
			PersistServer.activate("Maxis");
			PersistServer.beginTransaction();

			String jdbcDriver = new String();
			String jdbcDataSource = new String();
			String username = new String();
			String password = new String();

			com.checkfree.isolutions.property.Properties appCfg = com.checkfree.isolutions.property.PropertiesConfig.getApplicationSettings();

			if (appCfg.hasProperty("JDBCDriverName") == false)
				throw new IllegalArgumentException("JDBC Driver not specified!");
			else
				jdbcDriver = appCfg.getProperty("JDBCDriverName");

			if (appCfg.hasProperty("JDBCDataSourceName") == false)
				throw new IllegalArgumentException("JDBC Data Source not specified!");
			else
				jdbcDataSource = appCfg.getProperty("JDBCDataSourceName");

			if (appCfg.hasProperty("DataBaseUserid") == false)
				throw new IllegalArgumentException("Database userid not specified!");
			else
				username = appCfg.getProperty("DataBaseUserid");

			if (appCfg.hasProperty("DataBasePassword") == false)
				throw new IllegalArgumentException("Database password not specified!");
			else
				password = appCfg.getProperty("DataBasePassword");

			System.out.println("Initializing MyMaxis Customer Application Connection Pool.....");

			int initialConnections = 5;
			int maxConnections = 10;

			if (appCfg.hasProperty("CusInitialConnections") == false)
				initialConnections = 25;
			else
				initialConnections =  Integer.parseInt(appCfg.getProperty("CusInitialConnections"));

			if (appCfg.hasProperty("CusMaxConnections") == false)
				maxConnections = 50;
			else
				maxConnections = Integer.parseInt(appCfg.getProperty("CusMaxConnections"));

			connectionPool = new ConnectionPool(jdbcDriver, jdbcDataSource, username, password, initialConnections, maxConnections, true);

			connectionPoolStarted = true;

			System.out.println("MyMaxis Customer Application Connection Pool started");

			PersistServer.commitTransaction();

		} catch(SQLException sqle) {
			System.out.println("MyMaxis Customer Application Connection Pool failed to start :" + sqle);
			try{
				PersistServer.rollbackTransaction();
			}
			catch (CFiError cfie1) {
				System.out.println("Unable to rollback transaction (1):" + cfie1);
			}

		}
        catch(Exception e) {
            System.out.println("MyMaxis Customer Application Connection Pool failed to start :" + e);
            e.printStackTrace();
			try{
				PersistServer.rollbackTransaction();
			}
			catch (CFiError cfie2) {
				System.out.println("Unable to rollback transaction (2):" + cfie2);
			}
        }
		finally {
			PersistServer.deactivate();
		}

	}

	public void destroy() {
		System.out.println("Closing all Database Pool Threads.....");
		connectionPool.closeAllConnections();
		System.out.println("MyMaxis Customer Application Servlet - Shutdown.....");
	}

   public void doGet(HttpServletRequest request,
   		       HttpServletResponse response)
     throws ServletException, IOException
   {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.print("Copyright (c) 2002 FSBM CTech Sdn Bhd (427095-M).<br>");
		out.print("All rights reserved.<br>");
		out.print("<br>");
		if (connectionPoolStarted)
			out.print("MyMaxis Customer Application Connection Pool started.<br>");
		else
			out.print("Failed to start MyMaxis Customer Application Connection Pool.<br>");
		out.close();
   }
}

